package cn.com.blueInfo.autoCode.vue;

import lombok.Data;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @author suxch
 * @since 2025/9/14 22:59
 */
@Component
public class CreateIndexVue {

    private static final String BASE_VUE_FILE_PATH = "D:\\Development Program\\ProjectCode_idea\\LanxiPavilion\\LanxiPavilion-parent" +
            "\\lanxi-pavilion-vite-vue3\\src\\views\\bpm\\";
    private static final Map<String, String> MODULE_MAP = Collections.unmodifiableMap(new HashMap<String, String>() {{
        put("bsn", "number");
        put("osm", "person");
        put("sys", "system");
        put("uniflow", "template");
    }});

    private static final String[] FIELDS = {"application_id", "create_time", "create_user_id", "deleted", "delete_time",
            "delete_user_id", "extend1", "extend2", "extend3", "id", "order_number", "remark", "status", "tenant_id",
            "update_time", "update_user_id"};

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void handler() {
        String querySql = "select table_name, table_comment from information_schema.`TABLES` where TABLE_SCHEMA = 'haiyuan_dev'";
        List<Map<String, Object>> resultData = jdbcTemplate.queryForList(querySql);

        List<ListVueFile> listVueFileList = new ArrayList<>();

        for (Map<String, Object> oneData : resultData) {
            String tableName = (String) oneData.get("table_name");
            String[] tableNames = tableName.split("_");
            String tableComment = (String) oneData.get("table_comment");

            ListVueFile listVueFile = new ListVueFile();
            String modulePath = MODULE_MAP.get(tableNames[1]);
            listVueFile.setFilePath(BASE_VUE_FILE_PATH.concat(modulePath).concat(File.separator));
            listVueFile.setFileContent(getFileContent(getScriptContent(), getTemplateContent(tableComment), getStyleContent()));
            listVueFile.setFileName(getFileName(tableNames));
            listVueFileList.add(listVueFile);
        }
        writeFile(listVueFileList);
    }

    private String getScriptContent() {
        return "import {ref} from \"vue\";\n" +
                "import BaseDrawer from \"@/components/drawer/BaseDrawer.vue\";\n" +
                "import QueryDrawer from \"@/components/drawer/QueryDrawer.vue\";\n" +
                "import List from \"./List.vue\";\n" +
                "import Form from \"./Form.vue\";\n" +
                "import Query from \"./Query.vue\";\n" +
                "\n" +
                "const saveOrUpdateVisible = ref(false)\n" +
                "const queryVisible = ref(false)\n" +
                "\n" +
                "const handleDrawerSubmit = () => {\n" +
                "  saveOrUpdateVisible.value = false\n" +
                "  console.log(\"提交抽屉数据\")\n" +
                "}\n" +
                "\n" +
                "const openSaveOrUpdateDrawer = () => {\n" +
                "  saveOrUpdateVisible.value = true\n" +
                "  console.log(\"打开抽屉\")\n" +
                "}\n" +
                "\n" +
                "const handleExport = () => {\n" +
                "  console.log(\"导出功能\")\n" +
                "}\n" +
                "\n" +
                "const handleQuery = () => {\n" +
                "  queryVisible.value = false;\n" +
                "  console.log(\"执行高级查询\")\n" +
                "}\n" +
                "\n" +
                "const openQueryDrawer = () => {\n" +
                "  queryVisible.value = true;\n" +
                "  console.log(\"打开高级查询\")\n" +
                "}";
    }

    private String getTemplateContent(String tableComment) {
        return "  <el-button-group>\n" +
                "    <el-button @click=\"openQueryDrawer\">高级查询</el-button>\n" +
                "    <el-button @click=\"openSaveOrUpdateDrawer\">新增</el-button>\n" +
                "    <el-button @click=\"handleExport\">导出</el-button>\n" +
                "  </el-button-group>\n" +
                "  <List></List>\n" +
                "  <BaseDrawer\n" +
                "    v-model:visible=\"saveOrUpdateVisible\"\n" +
                "    title=\"新增" + tableComment + "\"\n" +
                "    @submit=\"handleDrawerSubmit\"\n" +
                "  >\n" +
                "    <template #saveOrUpdate>\n" +
                "      <Form suxch=\"苏希辰\"></Form>\n" +
                "    </template>\n" +
                "  </BaseDrawer>\n" +
                "  <QueryDrawer\n" +
                "    v-model:visible=\"queryVisible\"\n" +
                "    @query=\"handleQuery\"\n" +
                "  >\n" +
                "    <template #queryParam>\n" +
                "      <Query></Query>\n" +
                "    </template>\n" +
                "  </QueryDrawer>";
    }

    private String getStyleContent() {
        return ".el-button-group {\n" +
                "  position: absolute;\n" +
                "  top: 80px;\n" +
                "  right: 28px;\n" +
                "}";
    }

    private String getFileContent(String scriptContent, String templateContent, String styleContent) {
        return "<script setup>\n" +
                scriptContent + "\n" +
                "</script>\n" +
                "\n" +
                "<template>\n" +
                templateContent +
                "</template>\n" +
                "\n" +
                "<style scoped lang=\"scss\">\n" +
                styleContent + "\n" +
                "</style>";
    }

    private String getFileName(String[] tableNames) {
        String fileName = "";
        for (int t_i = 2, t_len = tableNames.length; t_i < t_len; t_i++) {
            String name = tableNames[t_i];
            fileName = fileName.concat(StringUtils.capitalize(name));
        }
        fileName = StringUtils.uncapitalize(fileName);
        return fileName.concat(File.separator).concat("index.vue");
    }

    private void writeFile(List<ListVueFile> vueFileList) {
        try {
            for (ListVueFile vueFile : vueFileList) {
                String filePath = vueFile.getFilePath().concat(vueFile.getFileName());
                FileUtils.writeStringToFile(new File(filePath), vueFile.getFileContent(), StandardCharsets.UTF_8);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Data
    private static class ListVueFile {
        private String fileName;
        private String filePath;
        private String fileContent;
    }

}
